﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using WWLauncher.ViewModel;
using Color = System.Windows.Media.Color;


namespace WWLauncher.View.Other
{

    public enum ColorType
    {
        Color1 = 1,
        Color2 = 2,
        TextColor = 3
    }

    /// <summary>
    /// MyColorPickerDialog.xaml 的交互逻辑
    /// </summary>
    public partial class MyColorPickerDialog : IDisposable
    {
        private AppConfig appConfig = MainViewModel.Instance().AppConfig;

        private HandyControl.Controls.Dialog dialog;
        private ToggleButton toggleButton;

        private ColorType colorType;


        public MyColorPickerDialog(string strType, string token)
        {
            InitializeComponent();
            switch (strType)
            {
                case "Color1":
                    colorType = ColorType.Color1;
                    break;
                case "Color2":
                    colorType = ColorType.Color2;
                    break;
                default:
                    colorType = ColorType.TextColor;
                    break;
            }
            dialog = HandyControl.Controls.Dialog.Show(this, token);
            this.Loaded += MyColorPickerDialog_Loaded;
        }

        private void MyColorPickerDialog_Loaded(object sender, RoutedEventArgs e)
        {
            if (!this.TextColor.IsNullOrEmpty())
            {
                var ctl = ColorTranslator.FromHtml(this.TextColor);
                Color color = Color.FromArgb(ctl.A, ctl.R, ctl.G, ctl.B);
                MyColorPicker.SelectedBrush = new SolidColorBrush(color);
            }
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyColorPicker_Canceled(object sender, EventArgs e)
        {
            MyColorPickerClose(sender);
        }
        private void MyColorPicker_Confirmed(object sender, HandyControl.Data.FunctionEventArgs<Color> e)
        {
            MyColorPickerClose(sender);
        }

        private void MyColorPicker_SelectedColorChanged(object sender, HandyControl.Data.FunctionEventArgs<Color> e)
        {
            SolidColorBrush scb = MyColorPicker.SelectedBrush;
            Color c = scb.Color;
            switch (colorType)
            {
                case ColorType.Color1:
                    appConfig.ThemeConfig.GradientBgParam.Color1 = string.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B); break;
                case ColorType.Color2:
                    appConfig.ThemeConfig.GradientBgParam.Color2 = string.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B); break;
                default:
                    appConfig.ThemeConfig.TextColor = string.Format("#{0:X2}{1:X2}{2:X2}", c.R, c.G, c.B); break;
            }
        }

        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Window.GetWindow(this).DragMove();
            }
        }


        private void MyColorPicker_Checked(object sender, RoutedEventArgs e)
        {
            toggleButton = e.OriginalSource as ToggleButton;

            PixelColorPickerWindow colorPickerWindow = new PixelColorPickerWindow(MyColorPicker);
            colorPickerWindow.Show();
        }

        private void MyColorPickerClose(object sender)
        {
            dialog.Close();
        }

        public void Dispose()
        { 
        }
        /// <summary>
        /// 16进制颜色文字
        /// </summary>
        public string TextColor { get; set; }
    }
}
